import React from 'react';
import { observer, inject } from 'mobx-react';
import * as Util from '../../util/formUtil';
import * as DetailUtil from '../../util/public/detailUtil';
import WeaSapBrowser from '../../public/wea-sap-browser/multi';
import * as _ from '../../util/public/common';
import { getEcCom,getLabel } from '../../util/convert';
import * as FieldUtil from '../../util/public/fieldUtil';

@inject('globalStore', 'layoutStore')
@observer
export default class DetailButton extends React.Component{
    constructor(props){
        super(props);
    }
    render(){
        const { WeaDropdown } = getEcCom();
        const { ButtonSelect } = WeaDropdown;
        const { symbol,globalStore,layoutStore } = this.props;
        const { JSComplete,commonParam } = globalStore;
        const { requestid,workflowid } = commonParam;
        const canEdit = parseInt(commonParam.isviewonly || 0) !== 1 && parseInt(commonParam.isaffirmance || 0) !== 1;
        if(!canEdit)
            return <div className="detailButtonDiv"></div>
        const { detailMap,mainData } = layoutStore;
        const detailObj = detailMap.get(symbol) || {};
        const checkedRowArr = DetailUtil.getDetailCheckedRowArr(detailObj);
        const disabledClassName = checkedRowArr.length > 0 ? "" : "detailBtnDisabled";
        const { attr,dropBtnKey } = detailObj;
        const { isadd,iscopy,isdelete,isopensapmul,sapProps } = attr;
        const showSap = isopensapmul === 1 && !_.isEmpty(sapProps);
        let dataParams = {requestid, workflowid};
        if(showSap){
            const { fielddbtype,relateFields,displaySearchAdInBar} = sapProps;
            dataParams["fielddbtype"] = fielddbtype;
            dataParams["displaySearchAdInBar"] = displaySearchAdInBar;
            relateFields && relateFields.map(fid =>{
                const fieldKey = `field${fid}`;
                const fieldValue = mainData.has(fieldKey) ? mainData.get(fieldKey).value : "";
                dataParams[fieldKey] = fieldValue;
            });
            if(relateFields)
                dataParams = {...dataParams, ...FieldUtil.getRelateFieldShowName(relateFields, -1)};
        }
        const detailIndex = parseInt(symbol.substring(7)) - 1;
        const addBtn = (
            <i 
                id={`addbutton${detailIndex}`} 
                name={`addbutton${detailIndex}`} 
                className={`icon-coms-Add-to-hot detailBtn`}
                title={getLabel(611,"添加")} 
                onClick={()=>{DetailUtil.addRow(symbol)}} 
            />
        );
        const delBtn = (
            <i 
                id={`delbutton${detailIndex}`} 
                name={`delbutton${detailIndex}`} 
                className={`icon-coms-form-delete-hot detailBtn ${disabledClassName}`}
                title={getLabel(91,"删除")} 
                onClick={()=>{Util.delRow(symbol)}} 
            />
        );
        const copyBtn = (
            <i 
                id={`copybutton${detailIndex}`} 
                name={`copybutton${detailIndex}`} 
                className={`icon-coms-copy-hot detailBtn ${disabledClassName}`}
                title={getLabel(77,"复制")} 
                onClick={()=>{DetailUtil.copyDetailRow(symbol)}} 
            />
        );

        let btns = [];
        if(showSap)
            btns.push(<WeaSapBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaSapBrowser@gmr42n@${symbol}`} dataParams={dataParams} onChange={this.doSapBrowAddRow} />);
        if(isadd === 1){
            btns.push(addBtn);
            if(iscopy === 1 && isdelete !== 1){
                const dropBtns = [
                    { key:"1", text:getLabel(91,"删除"), show:<span className={`icon-coms-form-delete-hot detailBtn ${disabledClassName}`} /> },
                    { key:"2", text:getLabel(77,"复制"), show:<span className={`icon-coms-copy-hot detailBtn ${disabledClassName}`} />},
                ];
                btns.push(<ButtonSelect ecId={`${this && this.props && this.props.ecId || ''}_ButtonSelect@zcycki@${symbol}`}
                    type="small"
                    selectedKey={dropBtnKey.toString()}
                    btnProps={{
                      style: {padding: 0, background: 'transparent', border: 'none', color: '#dadada'}
                    }}
                    datas={dropBtns}
                    btnOnClick={key => {
                        if(key == "1"){
                            Util.delRow(symbol);
                        }else if(key == "2"){
                            DetailUtil.copyDetailRow(symbol);
                        }
                    }}
                    menuOnClick={(key, e) => {
                        detailObj.setDropBtnKey(parseInt(key));
                    }}
                />);
            }else{
                btns.push(delBtn);
                iscopy === 1 && btns.push(copyBtn);
            }
        }else if(isdelete === 1){
            btns.push(delBtn);
        }
        return (
            <div className="detailButtonDiv">
                { btns }
            </div>
        );
    }
    doSapBrowAddRow = (datas) => {
        const { symbol } = this.props;
        datas && datas.map(addRowData =>{
            DetailUtil.addRow(symbol, addRowData, false);
        });
    }
}